define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'core/index',
                    add_url: 'core/add',
                    edit_url: 'core/edit',
                    del_url: 'core/del',
                    multi_url: 'core/multi',
                    table: 'core',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'department', title: __('Department')},
                        {field: 'person', title: __('Person')},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'text', title: __('Text')},
                        {field: 'user', title: __('User')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

        },
        edit: function () {
            Controller.api.bindevent();
            Controller.api.week();

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            week: function () {
                setTimeout(function(){
                    $("#c-year").selectpicker('refresh');
                },500);
                var DefaultsTime = $("#DefaultsTime").val();
                $(document).on("change", "#c-year", function () {
                    setTimeout(function(){
                        $("#c-date").selectpicker('val',DefaultsTime);
                        $("#c-date").selectpicker("refresh");
                        $("#c-date").selectpicker();
                    },500);
                });
            }
        }
    };
    return Controller;
});